package com.iotteam.androidbase.commen.views;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import com.iotteam.androidbase.R;
import com.iotteam.androidbase.base.BaseApplication;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by lidechen on 5/7/17.
 */

public class TimerButton extends Button implements View.OnClickListener {

    private Context mContext;

    private static final int MAX_TIME = 500;

    private static final int STATUS_PREPARE = 1;
    private static final int STATUS_TIMERRUNING = 2;
    private static final int STATUS_TIMERSTOP = 3;

    private static final int COLOR_ENABLE = BaseApplication.getAppContext().getResources().getColor(R.color.colorPrimary);
    private static final int COLOR_DISENABLE = BaseApplication.getAppContext().getResources().getColor(R.color.colorDisable);

    private int mBackgroundColor = COLOR_ENABLE;

    private int mStatus = STATUS_PREPARE;

    private String mMainText;
    private String mTimeoutText;

    /** 默认60s */
    private int mTotleTime = MAX_TIME;

    private Timer mTimer;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            TimerButton.this.setText(mTotleTime+" 秒");
            if(mTotleTime == 0){
                mStatus = STATUS_TIMERSTOP;
                onTimerEnd();
                mTimer.cancel();
                mTimer = null;
            }
        }
    };

    public TimerButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public TimerButton(Context context) {
        super(context);
        mContext = context;
        init();
    }

    private void init(){
        setBackgroundColor(mBackgroundColor);
        setTextColor(mContext.getResources().getColor(R.color.colorLightText));
        setOnClickListener(this);
    }

    private void startTimer(){

        onTimerStart();
        if(mTimer != null){
            mTimer.cancel();
        }
        mTimer = new Timer();
        mTotleTime = MAX_TIME;
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mTotleTime--;
                mHandler.sendEmptyMessage(0);
            }
        }, 1000, 1000);
        mStatus = STATUS_TIMERRUNING;
    }

    private void onTimerStart(){
        TimerButton.this.setText(mTotleTime+" 秒");
        setEnabled(false);
        setBackgroundColor(COLOR_DISENABLE);
    }

    private void onTimerEnd(){
        setText(mTimeoutText);
        setEnabled(true);
        setBackgroundColor(COLOR_ENABLE);
    }

    @Override
    public void onClick(View view) {
        if(mStatus == STATUS_PREPARE){
            if(mOnTimerStartListener != null){
                if(mOnTimerStartListener.onStart()){
                    startTimer();
                }
            }
        }else if(mStatus == STATUS_TIMERSTOP){
            if(mOnTimerStartListener != null){
                if(mOnTimerStartListener.onStart()){
                    startTimer();
                }
            }
        }else if(mStatus == STATUS_TIMERRUNING){

        }
    }

    public void releas(){
        if(mTimer != null){
            mTimer.cancel();
            mTimer = null;
        }
    }

    public void setMainText(String text){
        mMainText = text;
        setText(mMainText);
    }

    public void setTimeoutText(String text){
        mTimeoutText = text;
    }

    private OnTimerListener mOnTimerStartListener = null;

    public void setOnTimerListener(OnTimerListener onTimerStartListener){
        mOnTimerStartListener = onTimerStartListener;
    }

    public interface OnTimerListener{
        boolean onStart();
        void onStop();
    }
}
